'use strict';
// 添加初始化数据
const ulid = require('./ulid');
const md5 = require('md5');
const path = require('path');
const fs = require('fs');
const {getMenus,addMenus,getMenuNames} = require('./addmenu');
async function initDate(app) {
  // await InitTr069Command(app);
  const menuResult = await app.modelmysql.Menu.findAll();
  
  if (menuResult.length == 0) {
    addMenus(app);
  }
  let menus = getMenuNames();
  let needAddMenuNames = menus.filter(name=>menuResult.findIndex(model=>model.name===name)===-1);
  let needAddMenus = getMenus().filter(item=>needAddMenuNames.findIndex(name=>name===item.name)!==-1);
  // if(nee)
  needAddMenus.forEach(item=>{
    if(item.type != 1){
      let parent = menuResult.find(menu=>menu.name===item.parentName);
      item.module_id = parent.id ;
    }
  
  })
  await app.modelmysql.Menu.bulkCreate(needAddMenus);

  const password = md5('zhoukenan');
  const admin = await app.modelmysql.GlobalAdmin.findOne({
    where: {
      username: 'zhoukenan',
    },
  });
  if (admin) {
    return;
  }
  const result = await app.modelmysql.GlobalAdmin.findAll({ where: {
    username: 'zhoukenan',
  } });
  if (result.length == 0) {
    await app.modelmysql.GlobalAdmin.create({
      id: ulid.ulid(),
      username: 'zhoukenan',
      password,
      nickName: 'zhoukenan',
      language: 'en',
    });
  }
  const tenantresult = await app.modelmysql.TenantAdmin.findAll({
    where: {
      name: 'default',
    },
  });
  const tpassword = md5('admin');
  if (tenantresult.length == 0) {
    await app.modelmysql.TenantAdmin.create({
      id: ulid.ulid(),
      name: 'default',
      muser: 'admin',
      mpassword: tpassword,
      memo: 'default',
      enabled: true,
      initFlag: false,
    });
  }
  const tenantResult = await app.modelmysql.TenantAdmin.findOne({
    where: {
      name: 'default',
    },
  });
  const userresult = await app.modelmysql.UserAdmin.findAll({
    where: {
      username: 'admin',
    },
  });
  if (userresult.length == 0) {
    // TODO 这个方法需要改
    await app.modelmysql.UserAdmin.create({
      tenantadminId: tenantResult.id,
      username: 'admin',
      password: tpassword,
      nickname: 'admin',
      is_super: true,
      state: true,
      is_company: true,
      companyname: 'admin',
      language: 'en',
      outsidestaff: false,
      id: ulid.ulid(),
    });
  }
}
async function InitTr069Command(app) {
  const tenants = await app.model.TenantAdmin.findAll();
  const wanfixip = [];
  wanfixip.push({
    key: 'InternetGatewayDevice.WANDevice.1.WANConnectionDevice.$.WANIPConnection.$.X_RTK_VlanId',
    name: 'vlanNumber',
  });
  wanfixip.push({
    key: 'InternetGatewayDevice.WANDevice.1.WANConnectionDevice.$.WANIPConnection.$.AddressingType',
    name: 'AddressingType',
  });
  wanfixip.push({
    key: 'InternetGatewayDevice.WANDevice.1.WANConnectionDevice.$.WANIPConnection.$.ConnectionType',
    name: 'ConnectionType',
  });
  // addressingtype=static时 不配置DnsEnabled
  // wanfixip.push({
  //   key: 'InternetGatewayDevice.WANDevice.1.WANConnectionDevice.$.WANIPConnection.$.DNSEnabled',
  //   name: 'DNSEnabled',
  // });
  wanfixip.push({
    key: 'InternetGatewayDevice.WANDevice.1.WANConnectionDevice.$.WANIPConnection.$.DNSServers',
    name: 'dns',
  });
  wanfixip.push({
    key: 'InternetGatewayDevice.WANDevice.1.WANConnectionDevice.$.WANIPConnection.$.ExternalIPAddress',
    name: 'ip',
  });
  wanfixip.push({
    key: 'InternetGatewayDevice.WANDevice.1.WANConnectionDevice.$.WANIPConnection.$.SubnetMask',
    name: 'wanSubnetMask',
  });
  wanfixip.push({
    key: 'InternetGatewayDevice.WANDevice.1.WANConnectionDevice.$.WANIPConnection.$.DefaultGateway',
    name: 'wanGatewayIP',
  });
  wanfixip.push({
    key: 'InternetGatewayDevice.WANDevice.1.WANConnectionDevice.$.WANIPConnection.$.X_RTK_LanInterface',
    name: 'bindPortList',
  });
  wanfixip.push({
    key: 'InternetGatewayDevice.WANDevice.1.WANConnectionDevice.$.WANIPConnection.$.NATEnabled',
    name: 'NATEnabled',
  });
  wanfixip.push({
    key: 'InternetGatewayDevice.WANDevice.1.WANConnectionDevice.$.WANIPConnection.$.X_RTK_ServiceType',
    name: 'businesstype',
  });
  wanfixip.push({
    key: 'InternetGatewayDevice.WANDevice.1.WANConnectionDevice.$.WANIPConnection.$.Enable',
    name: 'Enable',
  });
  const lan = [];

  lan.push({
    key: 'InternetGatewayDevice.LANDevice.1.LANHostConfigManagement.IPInterface.1.IPInterfaceIPAddress',
    name: 'ip',
  });
  lan.push({
    key: 'InternetGatewayDevice.LANDevice.1.LANHostConfigManagement.IPRouters',
    name: 'ip',
  });
  lan.push({
    key: 'InternetGatewayDevice.LANDevice.1.LANHostConfigManagement.DHCPServerEnable',
    name: 'dhcp',
  });
  lan.push({
    key: 'InternetGatewayDevice.LANDevice.1.LANHostConfigManagement.DNSServers',
    name: 'dhcpdns',
  });
  lan.push({
    key: 'InternetGatewayDevice.LANDevice.1.LANHostConfigManagement.MaxAddress',
    name: 'maxAddress',
  });
  lan.push({
    key: 'InternetGatewayDevice.LANDevice.1.LANHostConfigManagement.MinAddress',
    name: 'minAddress',
  });
  const bridgearr = [];
  bridgearr.push({
    key: 'InternetGatewayDevice.WANDevice.1.WANConnectionDevice.$.WANIPConnection.$.ConnectionType',
    name: 'bridgetype',
  });
  bridgearr.push({
    key: 'InternetGatewayDevice.WANDevice.1.WANConnectionDevice.$.WANIPConnection.$.X_RTK_ServiceType',
    name: 'businesstype',
  });
  bridgearr.push({
    key: 'InternetGatewayDevice.WANDevice.1.WANConnectionDevice.$.WANIPConnection.$.X_RTK_VlanId',
    name: 'vlanNumber',
  });
  bridgearr.push({
    key: 'InternetGatewayDevice.WANDevice.1.WANConnectionDevice.$.WANIPConnection.$.X_RTK_LanInterface',
    name: 'bindPortList',
  });
  bridgearr.push({
    key: 'InternetGatewayDevice.WANDevice.1.WANConnectionDevice.$.WANIPConnection.$.ConnectionType',
    name: 'ConnectionType',
  });
  bridgearr.push({
    key: 'InternetGatewayDevice.WANDevice.1.WANConnectionDevice.$.WANIPConnection.$.Enable',
    name: 'Enable',
  });
  bridgearr.push({
    key: 'InternetGatewayDevice.WANDevice.1.WANConnectionDevice.$.WANPPPConnection.$.ConnectionType',
    name: 'bridgetype',
  });
  bridgearr.push({
    key: 'InternetGatewayDevice.WANDevice.1.WANConnectionDevice.$.WANPPPConnection.$.X_RTK_ServiceType',
    name: 'businesstype',
  });
  bridgearr.push({
    key: 'InternetGatewayDevice.WANDevice.1.WANConnectionDevice.$.WANPPPConnection.$.X_RTK_VlanId',
    name: 'vlanNumber',
  });
  bridgearr.push({
    key: 'InternetGatewayDevice.WANDevice.1.WANConnectionDevice.$.WANPPPConnection.$.X_RTK_LanInterface',
    name: 'bindPortList',
  });
  bridgearr.push({
    key: 'InternetGatewayDevice.WANDevice.1.WANConnectionDevice.$.WANPPPConnection.$.ConnectionType',
    name: 'ConnectionType',
  });
  bridgearr.push({
    key: 'InternetGatewayDevice.WANDevice.1.WANConnectionDevice.$.WANPPPConnection.$.Enable',
    name: 'Enable',
  });
  const siparr = [];
  siparr.push({
    key: 'InternetGatewayDevice.Services.VoiceService.1.VoiceProfile.1.SIP.ProxyServer',
    name: 'sip1ServerAddress',
  });
  siparr.push({
    key: 'InternetGatewayDevice.Services.VoiceService.1.VoiceProfile.1.SIP.ProxyServerPort',
    name: 'sip1ServerPort',
  });
  siparr.push({
    key: 'InternetGatewayDevice.Services.VoiceService.1.VoiceProfile.1.SIP.OutboundProxy',
    name: 'outboundServerAddress',
  });
  siparr.push({
    key: 'InternetGatewayDevice.Services.VoiceService.1.VoiceProfile.1.SIP.OutboundProxyPort',
    name: 'outboundServerPort',
  });
  siparr.push({
    key: 'InternetGatewayDevice.Services.VoiceService.1.VoiceProfile.1.SIP.RegistrarServer',
    name: 'domain',
  });
  siparr.push({
    key: 'InternetGatewayDevice.Services.VoiceService.1.VoiceProfile.1.Line.1.SIP.AuthUserName',
    name: 'userInfo',
  });
  siparr.push({
    key: 'InternetGatewayDevice.Services.VoiceService.1.VoiceProfile.1.Line.1.SIP.AuthPassword',
    name: 'password',
  });
  siparr.push({
    key: 'InternetGatewayDevice.Services.VoiceService.1.VoiceProfile.1.Line.1.Enable',
    name: 'enable',
  });
  siparr.push({
    key: 'InternetGatewayDevice.Services.VoiceService.1.VoiceProfile.1.Line.1.DirectoryNumber',
    name: 'directoryNumber',
  });

  const wanDHCPArr = [];
  wanDHCPArr.push({
    key: 'InternetGatewayDevice.WANDevice.1.WANConnectionDevice.$.WANIPConnection.$.X_RTK_ServiceType',
    name: 'businesstype',
  });
  wanDHCPArr.push({
    key: 'InternetGatewayDevice.WANDevice.1.WANConnectionDevice.$.WANIPConnection.$.X_RTK_VlanId',
    name: 'vlanNumber',
  });
  wanDHCPArr.push({
    key: 'InternetGatewayDevice.WANDevice.1.WANConnectionDevice.$.WANIPConnection.$.X_RTK_LanInterface',
    name: 'bindPortList',
  });
  wanDHCPArr.push({
    key: 'InternetGatewayDevice.WANDevice.1.WANConnectionDevice.$.WANIPConnection.$.AddressingType',
    name: 'AddressingType',
  });
  wanDHCPArr.push({
    key: 'InternetGatewayDevice.WANDevice.1.WANConnectionDevice.$.WANIPConnection.$.ConnectionType',
    name: 'ConnectionType',
  });
  wanDHCPArr.push({
    key: 'InternetGatewayDevice.WANDevice.1.WANConnectionDevice.$.WANIPConnection.$.NATEnabled',
    name: 'NATEnabled',
  });
  wanDHCPArr.push({
    key: 'InternetGatewayDevice.WANDevice.1.WANConnectionDevice.$.WANIPConnection.$.Enable',
    name: 'Enable',
  });
  const pppoeArr = [];
  pppoeArr.push({
    key: 'InternetGatewayDevice.WANDevice.1.WANConnectionDevice.$.WANPPPConnection.$.Username',
    name: 'pppoeusername',
  });
  pppoeArr.push({
    key: 'InternetGatewayDevice.WANDevice.1.WANConnectionDevice.$.WANPPPConnection.$.Password',
    name: 'pppoepassword',
  });
  pppoeArr.push({
    key: 'InternetGatewayDevice.WANDevice.1.WANConnectionDevice.$.WANPPPConnection.$.X_RTK_ServiceType',
    name: 'businesstype',
  });
  pppoeArr.push({
    key: 'InternetGatewayDevice.WANDevice.1.WANConnectionDevice.$.WANPPPConnection.$.X_RTK_VlanId',
    name: 'vlanNumber',
  });
  pppoeArr.push({
    key: 'InternetGatewayDevice.WANDevice.1.WANConnectionDevice.$.WANPPPConnection.$.X_RTK_LanInterface',
    name: 'bindPortList',
  });
  pppoeArr.push({
    key: 'InternetGatewayDevice.WANDevice.1.WANConnectionDevice.$.WANPPPConnection.$.NATEnabled',
    name: 'NATEnabled',
  });
  pppoeArr.push({
    key: 'InternetGatewayDevice.WANDevice.1.WANConnectionDevice.$.WANPPPConnection.$.ConnectionType',
    name: 'ConnectionType',
  });
  pppoeArr.push({
    key: 'InternetGatewayDevice.WANDevice.1.WANConnectionDevice.$.WANPPPConnection.$.Enable',
    name: 'Enable',
  });
  const wifi24Arr = [];
  wifi24Arr.push({
    key: 'InternetGatewayDevice.LANDevice.1.WLANConfiguration.6.Enable',
    name: 'wifistate',
  });
  wifi24Arr.push({
    key: 'InternetGatewayDevice.LANDevice.1.WLANConfiguration.6.SSID',
    name: 'ssidName',
  });
  wifi24Arr.push({
    key: 'InternetGatewayDevice.LANDevice.1.WLANConfiguration.6.X_RZ_BandWidth',
    name: 'channelwide',
  });
  wifi24Arr.push({
    key: 'InternetGatewayDevice.LANDevice.1.WLANConfiguration.6.Channel',
    name: 'channel',
  });
  wifi24Arr.push({
    key: 'InternetGatewayDevice.LANDevice.1.WLANConfiguration.6.BeaconType',
    name: 'encryption',
  });
  wifi24Arr.push({
    key: 'InternetGatewayDevice.LANDevice.1.WLANConfiguration.6.WPAAuthenticationMode',
    name: 'WPAAuthenticationMode',
  });
  wifi24Arr.push({
    key: 'InternetGatewayDevice.LANDevice.1.WLANConfiguration.6.WPAEncryptionModes',
    name: 'encryptionType',
  });
  wifi24Arr.push({
    key: 'InternetGatewayDevice.LANDevice.1.WLANConfiguration.6.PreSharedKey.1.KeyPassphrase',
    name: 'wpaSharedPassword',
  });
  wifi24Arr.push({
    key: 'InternetGatewayDevice.LANDevice.1.WLANConfiguration.6.AutoChannelEnable',
    name: 'AutoChannelEnable',
  });
  const wifi24Arr1 = [];
  wifi24Arr1.push({
    key: 'InternetGatewayDevice.LANDevice.1.WLANConfiguration.1.Enable',
    name: 'wifistate',
  });
  wifi24Arr1.push({
    key: 'InternetGatewayDevice.LANDevice.1.WLANConfiguration.1.SSID',
    name: 'ssidName',
  });
  wifi24Arr1.push({
    key: 'InternetGatewayDevice.LANDevice.1.WLANConfiguration.1.X_RZ_BandWidth',
    name: 'channelwide',
  });
  wifi24Arr1.push({
    key: 'InternetGatewayDevice.LANDevice.1.WLANConfiguration.1.Channel',
    name: 'channel',
  });
  wifi24Arr1.push({
    key: 'InternetGatewayDevice.LANDevice.1.WLANConfiguration.1.BeaconType',
    name: 'encryption',
  });
  wifi24Arr1.push({
    key: 'InternetGatewayDevice.LANDevice.1.WLANConfiguration.1.WPAAuthenticationMode',
    name: 'WPAAuthenticationMode',
  });
  wifi24Arr1.push({
    key: 'InternetGatewayDevice.LANDevice.1.WLANConfiguration.1.WPAEncryptionModes',
    name: 'encryptionType',
  });
  wifi24Arr1.push({
    key: 'InternetGatewayDevice.LANDevice.1.WLANConfiguration.1.PreSharedKey.1.KeyPassphrase',
    name: 'wpaSharedPassword',
  });
  wifi24Arr1.push({
    key: 'InternetGatewayDevice.LANDevice.1.WLANConfiguration.1.AutoChannelEnable',
    name: 'AutoChannelEnable',
  });
  const wifi58Arr = [];
  wifi58Arr.push({
    key: 'InternetGatewayDevice.LANDevice.1.WLANConfiguration.1.Enable',
    name: 'wifistate',
  });
  wifi58Arr.push({
    key: 'InternetGatewayDevice.LANDevice.1.WLANConfiguration.1.SSID',
    name: 'ssidName',
  });
  wifi58Arr.push({
    key: 'InternetGatewayDevice.LANDevice.1.WLANConfiguration.1.X_RZ_BandWidth',
    name: 'channelwide',
  });
  wifi58Arr.push({
    key: 'InternetGatewayDevice.LANDevice.1.WLANConfiguration.1.Channel',
    name: 'channel',
  });
  wifi58Arr.push({
    key: 'InternetGatewayDevice.LANDevice.1.WLANConfiguration.1.BeaconType',
    name: 'encryption',
  });
  wifi58Arr.push({
    key: 'InternetGatewayDevice.LANDevice.1.WLANConfiguration.1.WPAAuthenticationMode',
    name: 'WPAAuthenticationMode',
  });
  wifi58Arr.push({
    key: 'InternetGatewayDevice.LANDevice.1.WLANConfiguration.1.WPAEncryptionModes',
    name: 'encryptionType',
  });
  wifi58Arr.push({
    key: 'InternetGatewayDevice.LANDevice.1.WLANConfiguration.1.PreSharedKey.1.KeyPassphrase',
    name: 'wpaSharedPassword',
  });
  wifi58Arr.push({
    key: 'InternetGatewayDevice.LANDevice.1.WLANConfiguration.1.AutoChannelEnable',
    name: 'AutoChannelEnable',
  });
  const catvArr = [];
  catvArr.push({
    key: 'InternetGatewayDevice.X_RZ_Catv.RF_Enable',
    name: 'catvState',
  });
  catvArr.push({
    key: 'InternetGatewayDevice.X_RZ_Catv.PerformanceA',
    name: 'PerformanceA',
  });
  catvArr.push({
    key: 'InternetGatewayDevice.X_RZ_Catv.PerformanceB',
    name: 'PerformanceB',
  });
  const deviceTypes = [ 'ZX8101L', 'ZX8212DWT', 'ZX8112WT','ZX8414DWT'];
  const oldResult = await app.model.Tr069Command.findAll();
  for (let i = 0; i < tenants.length; i++) {
    if (oldResult.length === 0) {
      for (let j = 0; j < deviceTypes.length; j++) {
        const deviceType = deviceTypes[j];
        const obj = {
          id: ulid.ulid(),
          deviceType,
          typeName: 'WAN fix IP',
          keys: JSON.stringify(wanfixip),
          tid: tenants[i].id,
        };
        await app.model.Tr069Command.create(obj);
        const lanObj = {
          id: ulid.ulid(),
          deviceType,
          typeName: 'LAN',
          keys: JSON.stringify(lan),
          tid: tenants[i].id,
        };
        await app.model.Tr069Command.create(lanObj);
        const bridgeObj = {
          id: ulid.ulid(),
          deviceType,
          typeName: 'WAN Bridge',
          keys: JSON.stringify(bridgearr),
          tid: tenants[i].id,
        };
        await app.model.Tr069Command.create(bridgeObj);
        const sipObj = {
          id: ulid.ulid(),
          deviceType,
          typeName: 'SIP',
          keys: JSON.stringify(siparr),
          tid: tenants[i].id,
        };
        await app.model.Tr069Command.create(sipObj);
        const wanDHCPObj = {
          id: ulid.ulid(),
          deviceType,
          typeName: 'WAN DHCP',
          keys: JSON.stringify(wanDHCPArr),
          tid: tenants[i].id,
        };
        await app.model.Tr069Command.create(wanDHCPObj);
        const pppoeObj = {
          id: ulid.ulid(),
          deviceType,
          typeName: 'WAN PPPoE',
          keys: JSON.stringify(pppoeArr),
          tid: tenants[i].id,
        };
        await app.model.Tr069Command.create(pppoeObj);
        const wifi24Obj = {
          id: ulid.ulid(),
          deviceType,
          typeName: '2.4G wifi',
          keys: JSON.stringify(wifi24Arr),
          tid: tenants[i].id,
        };
        if (deviceType === 'ZX8112WT') {
          wifi24Obj.keys = JSON.stringify(wifi24Arr1);
        }
        await app.model.Tr069Command.create(wifi24Obj);
        const wifi58Obj = {
          id: ulid.ulid(),
          deviceType,
          typeName: '5.8g wifi',
          keys: JSON.stringify(wifi58Arr),
          tid: tenants[i].id,
        };
        await app.model.Tr069Command.create(wifi58Obj);
        const catvObj = {
          id: ulid.ulid(),
          deviceType,
          typeName: 'catv',
          keys: JSON.stringify(catvArr),
          tid: tenants[i].id,
        };
        await app.model.Tr069Command.create(catvObj);
      }
    }
  }
}

exports.initDate = initDate;
